/**
 *	Pacific PIR Script
 *	@author Gary Rios O'Hara
 */
 
(function($) {
/**
 *	jQuery background cover plugin
 *	@param {Object} options The options object
 *	@param {Boolean} center Horizontally center the image
 *	@param {Boolean} middle Vertically center the image
 *	@param {Number} width Image width
 *	@param {Number} height Image height
 */
$.fn.coverbg = function(options) {
	options = $.extend({
		center: false,
		middle: false,
		width: 800,
		height: 600
	}, options);
	
	return this.each(function() {
		var $this = $(this),
		$container = $this.parent(),
		center = options.center,
		middle = options.middle,
		img_aspect_ratio = options.height / options.width;
		
		function resize() {
			var margin_top = "",
				margin_left = "",
				container_aspect_ratio = $container.height() / $container.width();
			if(img_aspect_ratio >= container_aspect_ratio) {
				$this.css({"width": "100%", "height": "auto"});
				if(middle) margin_top = ($container.height() - $this.height()) / 2;
			}
			else {
				$this.css({"width": "auto", "height": "100%"});
				if(center) margin_left = ($container.width() - $this.width()) / 2;
			}
			if(center || middle) $this.css({"margin-top": margin_top, "margin-left": margin_left});
		}
		
		resize();
		$(window).on({resize: resize, load: resize});
	});
};
})(jQuery);

(function() {
	
var $imgBackground = $("img.background"),
	$tabs = $("div.tabs").find("a");

function changeBackground(delay) {
	setTimeout(function() {
		var length = $imgBackground.length,
			index = $imgBackground.filter(":visible").index() + 1;
		index = index >= length ? 0 : index;	
		$imgBackground.filter(":visible").fadeOut(4000);
		$imgBackground.eq(index).fadeIn(4000);
		changeBackground(delay);
	}, delay);
}

// fade
$("img").css("visibility", "hidden").imagesLoaded({
	progress: function(isBroken, $images, $proper, $broken) {
		var $this = $(this).css("visibility", "");
		if($.support.opacity) $this.css("opacity", 0).animate({"opacity": 1}, function() { $this.css({"opacity": ""}); if(!$this.attr("style")) $this.removeAttr("style"); });
	}
});

// fondo
$imgBackground.coverbg({center: true, middle: true, width:1600, height:800}).eq(Math.random() * 2).show();
//$imgBackground.coverbg({center: true, middle: true, width:1600, height:800}).eq(Math.random() * 3).show();
changeBackground(12000);

// servicios
$tabs.on("click", function() {
	var index = $.inArray(this, $tabs);
	$("div.tabs").hide();
	$("div.pane").eq(index).fadeIn();
});


// tabs
$("a.tabs-prev").on("click", function() {
	var index = $.inArray($(this).closest("div.pane")[0], $("div.pane"));
	index--;
	if(index < 0) index = $("div.pane").length - 1;
	$(this).closest("div.pane").hide();
	$("div.pane").eq(index).fadeIn();
});
$("a.tabs-next").on("click", function() {
	var index = $.inArray($(this).closest("div.pane")[0], $("div.pane"));
	index++;
	if(index >= $("div.pane").length) index = 0;
	$(this).closest("div.pane").hide();
	$("div.pane").eq(index).fadeIn();
});

// scrollbar
$("div.main").find("div.text").mCustomScrollbar({
	scrollButtons:{enable:true},
	advanced:{updateOnContentResize:true},
	theme:"dark-2"
});



// boton cerrar
$("a.close").on("click", function() {
	$(this).parent().hide();
	$("div.tabs").fadeIn();
});

})();